package com.banshuibao.bsb_android.adapter;

import android.app.Activity;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.ActionModel;
import com.banshuibao.bsb_android.model.ExpertAnswerDetailModel;
import com.banshuibao.bsb_android.model.ExpertResultModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.PriceUtil;
import com.banshuibao.bsb_android.util.ShareUtil;
import com.banshuibao.bsb_android.util.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2017/12/19.
 */

public class ExpertHeaderDetailAdapter extends RecyclerView.Adapter<ExpertHeaderDetailAdapter.MyViewHolder> {
    private final int HEADER_VIEW_USER = 0;
    private final int HEADER_VIEW_LIST = 1;
    private final int HEADER_VIEW_ACTION = 2;
    private final int BODY_VIEW = 3;
    private final Activity context;
    private List<DataViewHolder> list;
    private List<ExpertAnswerDetailModel.DataBean.AsksBean> asksBeans;
    private List<ExpertAnswerDetailModel.DataBean.VoicesBean> voicesBeans;
    private ExpertResultModel.DataBean dataBean;
    private ActionModel actionModel;
    private String uuid;
    private AnswerListenRecordAdapter answerListenRecordAdapter;
    private String isVip;

    public ExpertHeaderDetailAdapter(Activity context) {
        this.context = context;
        list = new ArrayList<>();
        isVip = GetLoginInfo.getLoginInfoInstance(context).readUser().getVip();
    }

    public void setUser(ExpertResultModel.DataBean dataBean) {
        this.dataBean = dataBean;
        this.uuid = dataBean.getUuid();
        setData();
    }

    public void setAction(ActionModel actionModel) {
        this.actionModel = actionModel;
        setData();
    }

    public void setHeader(List<ExpertAnswerDetailModel.DataBean.VoicesBean> voicesBeans) {
        this.voicesBeans = voicesBeans;
        setData();
    }

    public void setBody(List<ExpertAnswerDetailModel.DataBean.AsksBean> asksBeans) {
        this.asksBeans = asksBeans;
        setData();
    }

    public void setData() {
        if (this.list != null) {
            this.list.clear();
        }
        if (dataBean != null) {
            UserDataHolder userDataHolder = new UserDataHolder();
            userDataHolder.setObj(dataBean);
            list.add(userDataHolder);
        }
        if (voicesBeans != null) {
            //  for (int i = 0; i <voicesBeans.size() ; i++) {
            HeaderDataHolder headerDataHolder = new HeaderDataHolder();
            headerDataHolder.setObj(voicesBeans);
            list.add(headerDataHolder);
            //    }
        }
        if (actionModel != null) {
            ActionDataHolder actionDataHolder = new ActionDataHolder();
            actionDataHolder.setObj(actionModel);
            list.add(actionDataHolder);
        }
        if (asksBeans != null) {
            for (int i = 0; i < asksBeans.size(); i++) {
                BodyDataHolder bodyDataHolder = new BodyDataHolder();
                bodyDataHolder.setObj(asksBeans.get(i));
                list.add(bodyDataHolder);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case BODY_VIEW:
                View bodyView = View.inflate(context, R.layout.expert_answer_details_body_layout, null);
                return new BodyViewHolder(bodyView);
            case HEADER_VIEW_USER:
                View userView = View.inflate(context, R.layout.expert_answer_details_header_layout, null);
                return new HeaderViewHolder(userView);
            case HEADER_VIEW_LIST:
                View ListView = View.inflate(context, R.layout.detail_recorder_list_layout, null);
                return new ListViewHolder(ListView);
            case HEADER_VIEW_ACTION:
                View actionView = View.inflate(context, R.layout.detail_recorder_function_layout, null);
                return new ActionViewHolder(actionView);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            holder.setData(list.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }

    public abstract class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView) {
            super(itemView);
        }

        abstract void setData(DataViewHolder data);
    }

    public class HeaderViewHolder extends MyViewHolder {

        private TextView question, name, post,textView_price;
        private ImageView header_iv;
        TextView buy_tag;
        TextView buy_price;

        public HeaderViewHolder(View view) {
            super(view);
            textView_price = itemView.findViewById(R.id.buy);
            buy_tag = itemView.findViewById(R.id.buy_tag);
            buy_price = itemView.findViewById(R.id.buy_price);
            question = view.findViewById(R.id.question);
            name = view.findViewById(R.id.name);
            post = view.findViewById(R.id.post);
            header_iv = view.findViewById(R.id.header);
        }

        @Override
        void setData(DataViewHolder data) {
            UserDataHolder userDataHolder = (UserDataHolder) data;
            ExpertResultModel.DataBean dataBean = userDataHolder.getObj();
            if (dataBean != null) {
                if (dataBean.getQuPrice() == 0) {
                    buy_price.setVisibility(View.GONE);
                    textView_price.setTextColor(context.getResources().getColor(R.color.free_price));
                    textView_price.setText("限时免费听");
                } else {
                    buy_price.setVisibility(View.VISIBLE);
                    textView_price.setTextColor(context.getResources().getColor(R.color.charge_price));
                    if (isVip.equals("0")) {
                        buy_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
                        textView_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
                        textView_price.setText(PriceUtil.FormatDecimal(dataBean.getQuPrice()));
                        buy_tag.setVisibility(View.VISIBLE);
                    } else {
                        buy_tag.setVisibility(View.GONE);
                        buy_price.getPaint().setFlags(0);
                        textView_price.getPaint().setFlags(0);
                        buy_price.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                        textView_price.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
                        textView_price.setText(PriceUtil.FormatDecimal(dataBean.getQuPrice()));
                    }
                }
                question.setText(dataBean.getQuestion());
                name.setText(dataBean.getName());
                if(!TextUtils.isEmpty(dataBean.getDuty())){
                    post.setText(dataBean.getDuty());
                }else {
                    post.setText(dataBean.getPositiotn());
                }
                ImageUtil.loadImageHead(context, dataBean.getHeadImg(), header_iv);
            }

        }

    }

    public class BodyViewHolder extends MyViewHolder {

        TextView ask, answer, date;

        public BodyViewHolder(View itemView) {
            super(itemView);
            ask = itemView.findViewById(R.id.ask);
            answer = itemView.findViewById(R.id.answer);
            date = itemView.findViewById(R.id.recode_date);
        }

        @Override
        void setData(DataViewHolder data) {
            BodyDataHolder bodyDataHolder = (BodyDataHolder) data;
            ExpertAnswerDetailModel.DataBean.AsksBean asksBean = (ExpertAnswerDetailModel.DataBean.AsksBean) bodyDataHolder.getObj();
            if (asksBean != null) {
                ask.setText(asksBean.getContent());
                answer.setText(asksBean.getAnswer());
                date.setText(TimeUtil.format(asksBean.getCreateDate()));
            }
        }

    }

    public class ListViewHolder extends MyViewHolder {
        private RecyclerView recyclerView;
        public ListViewHolder(View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.recyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            answerListenRecordAdapter = new AnswerListenRecordAdapter(context);
            recyclerView.setAdapter(answerListenRecordAdapter);
        }

        @Override
        void setData(DataViewHolder data) {
            HeaderDataHolder headerDataHolder = (HeaderDataHolder) data;
            List<ExpertAnswerDetailModel.DataBean.VoicesBean> voicesBean = (List<ExpertAnswerDetailModel.DataBean.VoicesBean>) headerDataHolder.getObj();
            if (voicesBean != null) {
                answerListenRecordAdapter.setData(voicesBean,dataBean);
            }
        }
    }

    public class ActionViewHolder extends MyViewHolder {
        private TextView date, count, record;
        private ImageView like, report,share;

        public ActionViewHolder(View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.date);
          //  count = itemView.findViewById(R.id.count);
         //   like = itemView.findViewById(R.id.like);//赞
            report = itemView.findViewById(R.id.report);//举报
            record = itemView.findViewById(R.id.record);
            share = itemView.findViewById(R.id.share);
        }

        @Override
        void setData(DataViewHolder data) {
            final GetLoginInfo getLoginInfo = GetLoginInfo.getLoginInfoInstance(context).readUser();
            final ActionDataHolder actionDataHolder = (ActionDataHolder) data;
            final ActionModel actionModel = (ActionModel) actionDataHolder.getObj();
            if(asksBeans!=null&&asksBeans.size()>0){
                record.setVisibility(View.VISIBLE);
            }else{
                record.setVisibility(View.GONE);
            }
            if (actionModel != null) {
             //   count.setText("("+String.valueOf(actionModel.getCount())+")");
                date.setText(TimeUtil.format(actionModel.getDate()));
                report.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        IntentManager.goReportActivity(context, actionModel.getUuid(),actionModel.getTitle());
                    }
                });
                /**
                 * 分享
                 */
                share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(dataBean!=null){
                            ShareUtil.showShare(context, Constants.Url.EXPERT+uuid+"&firmId="+getLoginInfo.getFirmId()+"&inviteUserId="+getLoginInfo.getUserId(),dataBean.getQuestion(),"",null);
                        }
                    }
                });
            }
        }
    }

    public abstract class DataViewHolder<T> {
        abstract int getType();

        private T obj;

        public T getObj() {
            return obj;
        }

        public void setObj(T obj) {
            this.obj = obj;
        }
    }

    public class HeaderDataHolder extends DataViewHolder<List<ExpertAnswerDetailModel.DataBean.VoicesBean>> {

        @Override
        int getType() {
            return HEADER_VIEW_LIST;
        }
    }

    public class BodyDataHolder extends DataViewHolder<ExpertAnswerDetailModel.DataBean.AsksBean> {

        @Override
        int getType() {
            return BODY_VIEW;
        }
    }

    public class UserDataHolder extends DataViewHolder<ExpertResultModel.DataBean> {
        @Override
        int getType() {
            return HEADER_VIEW_USER;
        }
    }

    public class ActionDataHolder extends DataViewHolder<ActionModel> {
        @Override
        int getType() {
            return HEADER_VIEW_ACTION;
        }
    }

    public void clear(){
        if(answerListenRecordAdapter!=null){
            answerListenRecordAdapter.clear();
        }
    }
}
